package com.lfg.leetcode.leetcode0;


public class No41FirstMissingPositive {

    public static void main(String[] args) {
        int ret = firstMissingPositive(new int[]{1});
        System.out.println(ret);
    }

    public static int firstMissingPositive(int[] nums) {
        for (int i = 0; i < nums.length; ) {
            int num = nums[i] - 1;
            if (num < 0) {
                i++;
            } else if (num >= nums.length) {
                i++;
            } else if (num == i || nums[i] == nums[num]) {
                i++;
            } else {
                nums[i] = nums[num];
                nums[num] = num + 1;
            }
        }
        int i = 0;
        for (; i < nums.length; i++) {
            int i1 = i + 1;
            if (i1 != nums[i]) {
                return i1;
            }
        }

        return i + 1;
    }
}

